package com.xingchi.tornado.core.enums;

import com.xingchi.tornado.basic.BaseEnum;
import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 操作类型
 *
 * @author xingchi
 * @date 2025/7/3 11:18
 */
@Getter
public enum OperateTypeEnum implements BaseEnum {

    INSERT(1, "添加"),
    UPDATE(2, "更新"),
    DELETE(3, "删除"),
    QUERY(4, "查询"),
    IMPORT(5, "导入"),
    EXPORT(6, "导出"),
    OTHER(20, "其他"),
    ;
    private final Integer code;
    private final String desc;

    OperateTypeEnum(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    private static final Map<Integer, OperateTypeEnum> CODE_MAPPINGS = Arrays.stream(OperateTypeEnum.values())
            .collect(Collectors.toMap(OperateTypeEnum::getCode, Function.identity()));

    public static OperateTypeEnum getInstance(Integer code) {
        return CODE_MAPPINGS.get(code);
    }

    public static OperateTypeEnum getInstanceOrDefault(Integer code, OperateTypeEnum defaultEnum) {
        return CODE_MAPPINGS.getOrDefault(code, defaultEnum);
    }

    public static boolean exists(Integer code) {
        return CODE_MAPPINGS.containsKey(code);
    }

}
